package cc.rengu.redp.system.service.impl;

import cc.rengu.redp.system.dao.CommonHolidaysMapper;
import cc.rengu.redp.system.domain.HolidayInfo;
import cc.rengu.redp.system.domain.Holidays;
import cc.rengu.redp.system.service.CommonHolidaysService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @author qiujunning
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CommonHolidaysServiceImpl extends ServiceImpl<CommonHolidaysMapper, HolidayInfo> implements CommonHolidaysService {

    @Override
    public boolean isHoliday(LocalDate ld) {
        HolidayInfo holidayInfo = this.baseMapper.selectOne(
                new LambdaQueryWrapper<HolidayInfo>().eq(HolidayInfo::getBatchDate, ld.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")).replace("-", "")));
        if (holidayInfo != null) {
            if (holidayInfo.getHolidayFlag().equals("N")){
                return false;
            } else {
                return true;
            }
        } else {
            if (ld.getDayOfWeek().getValue() == 6 || ld.getDayOfWeek().getValue() == 7) {
                return true;
            } else {
                return false;
            }
        }
    }
}